﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Business = SoTaiChinh.Library.Business;
using Entity = SoTaiChinh.Library.Entity;
public partial class templates_client_usercontrols_poll : System.Web.UI.UserControl
{
    Entity.ThamDoYKien clsE_ThamDoYKien = new SoTaiChinh.Library.Entity.ThamDoYKien();
    Business.ThamDoYKien clsB_ThamDoYKien = new SoTaiChinh.Library.Business.ThamDoYKien();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DisplayPoll();
            ketqua.Visible = false;
        }
    }
    private void DisplayPoll()
    {
        try
        {
            rdbTraLoi.Items.Clear();         
            DataSet ds = clsB_ThamDoYKien.SelectCauHoi_TraLoi(clsE_ThamDoYKien);
            lbIdCauHoi.Text = ds.Tables[0].Rows[0]["Id"].ToString();
            lbCauHoi.Text = ds.Tables[0].Rows[0]["CauHoi"].ToString();
            int i = 0;
            foreach (DataRow dr in ds.Tables[1].Rows)
            {
                rdbTraLoi.Items.Add(dr["TraLoi"].ToString());
                rdbTraLoi.Items[i].Value = dr["Id"].ToString();
                //rdbTraLoi.SelectedIndex = 0;
                i++;
            }
        }
        catch (Exception ex) { }
    }
    private void BindItemsList()
    {
        DataTable dt = clsB_ThamDoYKien.SelectTraLoi_BinhChon(clsE_ThamDoYKien);
        if (dt.Rows.Count == 0)
        {
            lblTong.Text = "<strong>Chưa có bình chọn</strong> ";

        }
        else
        {
            lblTong.Text = "<strong>Tổng số bình chọn :</strong> " + dt.Rows[0]["Tong"].ToString() + " phiếu";
            rptKetQua.DataSource = dt.DefaultView;
            rptKetQua.DataBind();
        }
    }
    protected void btnDanhGia_Click(object sender, EventArgs e)
    {
        if (rdbTraLoi.SelectedItem != null)
        {
            clsE_ThamDoYKien.Id = Int32.Parse(rdbTraLoi.SelectedItem.Value);
            clsB_ThamDoYKien.UpdateBinhChon(clsE_ThamDoYKien);
            DisplayPoll();
        }
        else
        {
            Alert.Show("Bạn chưa chon câu trả lời để bình chọn !!!");
        }
    }
    protected void btnKetQua_Click(object sender, EventArgs e)
    {
        binhchon.Visible = false;
        BindItemsList();
        ketqua.Visible = true;
    }
    protected void btnQuayLai_Click(object sender, EventArgs e)
    {
        DisplayPoll();
        binhchon.Visible = true;
        ketqua.Visible = false;
    }
}
